import pygame
import random

# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GRAY = (128, 128, 128)

# 屏幕尺寸
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 800

# 方格大小
GRID_SIZE = 200

# 初始化 pygame
pygame.init()

#设置logo
icon = pygame.image.load('logo.png')
pygame.display.set_icon(icon)

# 创建屏幕
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("2048 游戏 Design by 白水鱼")

# 游戏网格
grid = [[0 for _ in range(4)] for _ in range(4)]

# 生成新数字
def generate_number():
    empty_cells = []
    for row in range(4):
        for col in range(4):
            if grid[row][col] == 0:
                empty_cells.append((row, col))
    if empty_cells:
        row, col = random.choice(empty_cells)
        grid[row][col] = 2 if random.random() < 0.9 else 4

# 检查是否可以移动
def can_move(direction):
    for row in range(4):
        for col in range(4):
            target_row, target_col = row, col
            if direction == "up":
                target_row -= 1
            elif direction == "down":
                target_row += 1
            elif direction == "left":
                target_col -= 1
            elif direction == "right":
                target_col += 1

            if target_row < 0 or target_row >= 4 or target_col < 0 or target_col >= 4:
                continue

            if grid[row][col] and grid[target_row][target_col] == grid[row][col]:
                return True
    return False

# 执行移动
def move(direction):
    moved = False
    if direction == "up":
        for col in range(4):
            merged = False
            for row in range(1, 4):
                if grid[row][col]!= 0:
                    for target_row in range(row - 1, -1, -1):
                        if grid[target_row][col] == 0:
                            grid[target_row][col] = grid[row][col]
                            grid[row][col] = 0
                            moved = True
                        elif grid[target_row][col] == grid[row][col] and not merged:
                            grid[target_row][col] *= 2
                            grid[row][col] = 0
                            merged = True
                            moved = True
                            break
    elif direction == "down":
        for col in range(4):
            merged = False
            for row in range(2, -1, -1):
                if grid[row][col]!= 0:
                    for target_row in range(row + 1, 4):
                        if grid[target_row][col] == 0:
                            grid[target_row][col] = grid[row][col]
                            grid[row][col] = 0
                            moved = True
                        elif grid[target_row][col] == grid[row][col] and not merged:
                            grid[target_row][col] *= 2
                            grid[row][col] = 0
                            merged = True
                            moved = True
                            break
    elif direction == "left":
        for row in range(4):
            merged = False
            for col in range(1, 4):
                if grid[row][col]!= 0:
                    for target_col in range(col - 1, -1, -1):
                        if grid[row][target_col] == 0:
                            grid[row][target_col] = grid[row][col]
                            grid[row][col] = 0
                            moved = True
                        elif grid[row][target_col] == grid[row][col] and not merged:
                            grid[row][target_col] *= 2
                            grid[row][col] = 0
                            merged = True
                            moved = True
                            break
    elif direction == "right":
        for row in range(4):
            merged = False
            for col in range(2, -1, -1):
                if grid[row][col]!= 0:
                    for target_col in range(col + 1, 4):
                        if grid[row][target_col] == 0:
                            grid[row][target_col] = grid[row][col]
                            grid[row][col] = 0
                            moved = True
                        elif grid[row][target_col] == grid[row][col] and not merged:
                            grid[row][target_col] *= 2
                            grid[row][col] = 0
                            merged = True
                            moved = True
                            break

        if moved:
            generate_number()

# 绘制网格
def draw_grid():
    for row in range(4):
        for col in range(4):
            pygame.draw.rect(screen, GRAY, [col * GRID_SIZE, row * GRID_SIZE, GRID_SIZE, GRID_SIZE], 1)
            if grid[row][col]!= 0:
                font = pygame.font.SysFont(None, 48)
                text = font.render(str(grid[row][col]), 1, WHITE)
                text_rect = text.get_rect(center=(col * GRID_SIZE + GRID_SIZE // 2, row * GRID_SIZE + GRID_SIZE // 2))
                screen.blit(text, text_rect)

# 游戏主循环
clock = pygame.time.Clock()
generate_number()
generate_number()
generate_number()
generate_number()


game_over = False
while not game_over:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_over = True

    keys = pygame.key.get_pressed()
    if keys[pygame.K_UP]:
        if can_move("up"):
            move("up")
    elif keys[pygame.K_DOWN]:
        if can_move("down"):
            move("down")
    elif keys[pygame.K_LEFT]:
        if can_move("left"):
            move("left")
    elif keys[pygame.K_RIGHT]:
        if can_move("right"):
            move("right")
    elif keys[pygame.K_RIGHT]:
        if can_move("right"):
            move("right")
    elif keys[pygame.K_n]:
        generate_number()



    screen.fill(BLACK)
    draw_grid()

    pygame.display.flip()
    clock.tick(10)

# 退出程序
pygame.quit()